#!/bin/bash

# Variables
CONTAINER_NAME="database_container"
SQL_FILE_PATH="./patch_multihandle_rating.sql"
DB_USER="root"
DB_NAME="acm_training_platform"

# New and old database passwords
NEW_PASSWORD='YT_69UK2:1Hj4)c@k9/NdhI5rmu0nvS['
OLD_PASSWORD_1='YT_69UK2:1Hj4)c@k9/NdhI5rmu0nvS['
OLD_PASSWORD_2='123456'

# Function to run SQL patch
run_sql_patch() {
    echo "Running SQL patch..."
    sudo docker exec -i "$CONTAINER_NAME" mysql -u "$DB_USER" -p"$NEW_PASSWORD" "$DB_NAME" < "$SQL_FILE_PATH"
    if [ $? -eq 0 ]; then
        echo "SQL patch executed successfully."
    else
        echo "Failed to execute SQL patch."
    fi
}

# Update password (try first old password)
if sudo docker exec -i "$CONTAINER_NAME" mysql -u root -p"$NEW_PASSWORD" -e "SELECT 1;" >/dev/null 2>&1; then
    echo "Password is OK."
    run_sql_patch
fi
